package com.example.exchangeapp.zjs;

import static android.content.ContentValues.TAG;

import static com.example.exchangeapp.yy.constant.AppId.AppId;
import static com.example.exchangeapp.yy.constant.AppSecret.appSecret;

import android.content.Context;
import android.content.Intent;
import android.os.NetworkOnMainThreadException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager2.widget.ViewPager2;
import androidx.recyclerview.widget.RecyclerView;

import com.example.exchangeapp.R;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ImageSliderAdapter extends RecyclerView.Adapter<ImageSliderAdapter.ImageSliderViewHolder> {

    private List<Integer> imageList;
    private Context context;
    private ViewPager2 viewPager;
    //轮播图单个商品详情
    private Product singleProduct;

    private final Gson gson = new Gson();

    public ImageSliderAdapter(Context context, List<Integer> imageList, ViewPager2 viewPager) {
        this.context = context;
        this.imageList = imageList;
        this.viewPager = viewPager;
    }

    @NonNull
    @Override
    public ImageSliderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_image_slider, parent, false);
        return new ImageSliderViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageSliderViewHolder holder, int position) {
        int imageRes = imageList.get(position);
        holder.imageView.setImageResource(imageRes);

        //轮播图点击监听事件
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int productId = 434;
                switch (imageRes) {
                    case R.drawable.image1:
                        productId = 434;
                        break;
                    case R.drawable.image2:
                        productId = 435;
                        break;
                    case R.drawable.image3:
                        productId = 438;
                        break;
                    default:
                        break;
                }
                //查询单个商品详情
                getSingleProduct(productId);
            }
        });
    }

    @Override
    public int getItemCount() {
        return imageList.size();
    }

    class ImageSliderViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        ImageSliderViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
        }
    }

    //get请求单个商品数据，用于轮播图
    private void getSingleProduct(Integer productId){
        new Thread(() -> {
            // 请求的url路径
            String url = "http://47.107.52.7:88/member/tran/goods/details?goodsId="+productId;

            //我的f9ed7014c8cb45169ab4cc0728775790和26073a2f31b8baca64eca93c3c2c5a3dd2313
            //yy的ea0f4556cf414613a551ca2c25840189和139172f805f8d3e654ffb891d963d96f64f62
            // 请求头
            Headers headers = new Headers.Builder()
                    .add("appId", AppId)
                    .add("appSecret", appSecret)
                    .add("Accept", "application/json, text/plain, */*")
                    .build();

            //请求组合创建
            Request request = new Request.Builder()
                    .url(url)
                    // 将请求头加至请求中
                    .headers(headers)
                    .get()
                    .build();
            try {
                OkHttpClient client = new OkHttpClient();
                //发起请求，传入callback进行回调
                client.newCall(request).enqueue(callbackSingleProduct);
            }catch (NetworkOnMainThreadException ex){
                ex.printStackTrace();
            }
        }).start();
    }

    private final Callback callbackSingleProduct = new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            Log.e(TAG, "连接服务器失败!");
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            // 获取响应体的json串
            String body = response.body().string();
            Log.d("json串为", body);
            // 解析json串到自己封装的状态
            ProductResponse2 dataResponseBody = gson.fromJson(body, ProductResponse2.class);
            Log.d("商品信息为", "商品信息为"+dataResponseBody.toString());

            // 获取商品详情
            singleProduct = dataResponseBody.getData();

            Intent intent = new Intent(context, ProductDetailActivity.class);
            intent.putExtra("product", singleProduct);
            context.startActivity(intent);
        }
    };
}
